const getLocation = (options) => {
  return new Promise((resolve, reject) => {
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        resolve(res)
      },
      fail: (err) => {
        let errorMsg = err.errMsg;
        let tips = ""
        if (errorMsg.indexOf("auth deny") !== -1 || errorMsg.indexOf("authorize no response") !== -1 ||
            errorMsg.indexOf("auth denied") !== -1) {
          tips = '由于您拒绝了授权,无法获取距离信息,如需获取,请开启位置权限后,请刷新重新获取定位';
        } else if (errorMsg.indexOf("getLocation:fail") !== -1) {
          tips = '您手机未开启定位功能，开启后请刷新重新获取定位';
        }
        if (options && options.tips && tips) {
          wx.showModal({
            title: '位置授权提示', content: tips, showCancel: false, success(res) {
              if (res.confirm) {
                reject()
              }
            }
          })
        } else {
          reject();
        }
      }
    })
  })
}
const authorize = (scope) => {
  return new Promise(((resolve, reject) => {
    wx.authorize({
      scope: scope,
      success(res) {
        resolve(res)
      },
      fail(error) {
        reject(error)
      }
    })
  }))
}

const getSetting = () => {
  return new Promise(((resolve, reject) => {
    wx.getSetting({
      success(res) {
        resolve(res)
      },
      fail(error) {
        reject(error)
      }
    })
  }))
}
const requestSubscribeMessage = (tmplIds) => {
  return new Promise(((resolve, reject) => {
    wx.requestSubscribeMessage({
      tmplIds: tmplIds,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  }))
}

export default {
  getLocation,
  requestSubscribeMessage,
  getSetting,
  authorize
}
